package com.dream.system.service.impl;

import java.text.ParseException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import com.dream.common.Common;
import com.dream.common.SuperDao;
import com.dream.model.SysUser;
import com.dream.model.Sys_Class;
import com.dream.model.Sys_ClassPlantMaster;
import com.dream.model.Sys_LearnPlant;
import com.dream.system.service.ISysClassPlant;

@Component("sysClassPlantImpl")
@SuppressWarnings("all")
public class SysClassPlantImpl extends SuperDao<Object> implements ISysClassPlant {

	/**
	 * 查询这个老师所在的学校
	 */
	@SuppressWarnings("all")
	@Override
	public Map getTeacherSchool(SysUser model) {
		// TODO Auto-generated method stub
		String opt = "t.table_id, t.name, t.cid, t.cpid, t.type, t.lv, t.note, t.timeLearn, t.addresss, t.cpassword, t.open, t.pName,t.showTime,t1.year,t1.state";
		String sql = "from  Sys_Class t left join t.listsuc t1  where (t.type='2' or t.type='4' or t.type='3') and t1.sysUser.table_id = ? and t1.state <> '-1' ";
		Object[] obj = new Object[] { model.getTable_id() };
		List<Sys_Class> list2 = (List<Sys_Class>) this.getList("select DISTINCT new com.dream.model.Sys_Class(" + opt + ") " + sql, obj, 0, 0, "");
		int count = this.getListCount("select count(*) " + sql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list2);
		map.put("sum", count);
		return map;
	}

	/**
	 * 增加一个老师的课排表
	 */
	@Override
	public int addClassPlantMaster(SysUser model, JSONArray jsonArray) {
		// TODO Auto-generated method stub

		for (int i = 0; i < jsonArray.size(); i++) {
			Sys_ClassPlantMaster sc = new Sys_ClassPlantMaster();
			JSONObject json = jsonArray.getJSONObject(i);
			sc.setSysUser(model);
			sc.setStart1(json.getString("start"));
			sc.setEnd(json.getString("end"));
			sc.setValue(json.getString("value"));
			Sys_Class sc1 = new Sys_Class();
			sc1.setTable_id(json.getString("tid").toString());
			sc.setSys_Class(sc1);
			sc.setSname(json.getString("sname"));
			sc.setWeek(json.getString("week"));
			String hql = "select count(*) from Sys_ClassPlantMaster t where t.start1 = ? and t.end = ? and t.week = ? and t.value = ? and t.sysUser.table_id = ? ";
			Object[] obj = new Object[] { json.getString("start"), json.getString("end"), json.getString("week"), json.getString("value"), model.getTable_id() };
			int count = this.getListCount(hql, obj, "");
			if (count > 0) {
				continue;
			}
			this.addT(sc);
		}
		return 1;
	}

	/**
	 * 查询老师的课表
	 */
	@SuppressWarnings("all")
	@Override
	public Map getTeacherPlantMaster(SysUser model) {
		// TODO Auto-generated method stub
		// t.table_id, t.start1, t.end, t.week, t.value, t.sname,t.classId 一次代码
		// t.table_id, t.start1, t.end, t.week, t.value, t.sname,t.classTime,
		// t.classTimeShow,t.classId,t.userId,t.learnPlantId 二次调整
		String sql = "select new com.dream.model.Sys_ClassPlantMaster(t.table_id, t.start1, t.end, t.week, t.value, t.sname,t.classTime, t.classTimeShow,t.sys_Class.table_id,t.sysUser.table_id,t1.table_id,t1.pName) from Sys_ClassPlantMaster t left join t.sys_LearnPlant t1 where t.sysUser.table_id = ? ";
		// String sql =
		// "select new com.dream.model.Sys_ClassPlantMaster(t.table_id, t.sysUser, t.sys_Class, t.start1, t.end, t.week, t.value, t.sname, t.sys_LearnPlant,t.classTime, t.classTimeShow) from Sys_ClassPlantMaster t where t.sysUser.table_id = ? ";

		Object[] obj = new Object[] { model.getTable_id() };
		List<Sys_ClassPlantMaster> list2 = (List<Sys_ClassPlantMaster>) this.getList(sql, obj, 0, 0, "");
		// int count = this.getListCount("select count(*) " + sql, obj, "");
		int count = 0;
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list2);
		map.put("sum", count);
		return map;
	}
    /**
     * 
     * 
     * 查询学生班的课表信息
     * 
     * 
     * 
     */
	@SuppressWarnings("all")
	@Override
	public Map getStudentPlantMaster(SysUser model) {
		// TODO Auto-generated method stub
		// String opt
		// ="t.table_id, t.name, t.cid, t.cpid, t.type, t.lv, t.note, t.timeLearn, t.addresss, t.cpassword, t.open, t.pName,t.showTime,t.string1,t.string2";
		// 这里把中间表的年代和状态，放在了string1,string2之中
		String opt = "t.table_id, t.name, t.cid, t.cpid, t.type, t.lv, t.note, t.timeLearn, t.addresss, t.cpassword, t.open, t.pName,t.showTime,t1.year,t1.state";
		String sql = "from Sys_Class t left join t.listsuc t1  where t.type='4' and t1.sysUser.table_id = ? and t1.state ='1' ";
		Object[] obj = new Object[] { model.getTable_id() };
		List<Sys_Class> list1 = (List<Sys_Class>) this.getList("select new com.dream.model.Sys_Class(" + opt + ") " + sql, obj, model.getStart(), model.getLimit(), "");
		int count = 0; // this.getListCount("select count(*) " + sql, obj, "");

		String str = "";
		for (int i = 0; i < list1.size(); i++) {
			Sys_Class obj1 = list1.get(i);
			if (i == list1.size() - 1) {
				str += "'" + obj1.getTable_id() + "'";
			} else {
				str += "'" + obj1.getTable_id() + "',";
			}
		}
		if("".equals(str)){
			return null;
		}
		String sql1 = "select new com.dream.model.Sys_ClassPlantMaster(t.table_id, t.start1, t.end, t.week, t.value, t.sname,t.sys_Class.table_id) from Sys_ClassPlantMaster t where t.sys_Class.table_id in (" + str + ") ";

		List<Sys_ClassPlantMaster> list2 = (List<Sys_ClassPlantMaster>) this.getList(sql1, 0, 0, "");

		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list2);
		map.put("sum", count);

		return map;
	}

	/**
	 * 查询一个人自己的课排表
	 */
	@Override
	@SuppressWarnings("all")
	public Map getTeacherClassPlant(SysUser model, Sys_LearnPlant slp) {
		// TODO Auto-generated method stub
		String name = "";
		if (null == slp || null == slp.getName()) {
			name = "";
		} else {
			name = slp.getName();
		}
		String dsql = "";
		Object[] obj = new Object[] { model.getTable_id() };
		dsql = " from Sys_LearnPlant t where t.pltype ='4' and t.sysUser.table_id = ?  and (t.name like '%" + name + "%' or t.pName like '%" + name + "%' ) ";
		@SuppressWarnings("unchecked")
		List<Sys_LearnPlant> list1 = (List<Sys_LearnPlant>) this.getList(dsql, obj, model.getStart(), model.getLimit(), "");
		int count = this.getListCount("select count(*) " + dsql, obj, "");
		Map map = new HashMap();
		map.put("count", count % model.getLimit() > 0 ? count / model.getLimit() + 1 : count / model.getLimit());
		map.put("list", list1);
		map.put("sum", count);
		return map;
	}

	/**
	 * 
	 * 
	 * 更新模板中具体哪一天上什么课。这个课在每周都会被清空一次
	 * 
	 * 
	 * 
	 */
	@Override
	public Sys_ClassPlantMaster updatePlantMasterValue(Sys_ClassPlantMaster model) {
		// TODO Auto-generated method stub

		if (null == model || null == model.getTable_id() || "".equals(model.getTable_id())) {
			return null;
		}

		Sys_ClassPlantMaster scp = (Sys_ClassPlantMaster) this.getT(new Sys_ClassPlantMaster(), model.getTable_id());

		if (scp == null) {
			return null;
		}
		scp.setSys_LearnPlant(model.getSys_LearnPlant());
		try {
			scp.setClassTime(Common.getClassTime(model.getWeek(), model.getStart1()));
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		scp.setClassTimeShow(Common.getClassTimeShow(model.getWeek(), model.getStart1()));
		this.updateT(scp);
		return scp;
	}

	@Override
	public int updateMasterWeekValue(SysUser model) {
		// TODO Auto-generated method stub
		String hql = "from Sys_ClassPlantMaster t where t.sysUser.table_id = ? ";
		Object[] obj = new Object[] { model.getTable_id() };
		List<Sys_ClassPlantMaster> list = (List<Sys_ClassPlantMaster>) this.getList(hql, obj, 0, 0, "");
		for (int i = 0; i < list.size(); i++) {
			Sys_ClassPlantMaster sc = list.get(i);
			sc.setSys_LearnPlant(null);
			sc.setClassTime(null);
			sc.setClassTimeShow(null);
			this.updateT(sc);
		} 
		return 1;

	}
	@Scheduled(cron="0 0 23 * * SAT")  
	public int updateMasterWeekValueAll(SysUser model) {
		// TODO Auto-generated method stub
		String hql = "from Sys_ClassPlantMaster t ";
		Object[] obj = new Object[] { model.getTable_id() };
		List<Sys_ClassPlantMaster> list = (List<Sys_ClassPlantMaster>) this.getList(hql, obj, 0, 0, "");
		for (int i = 0; i < list.size(); i++) {
			Sys_ClassPlantMaster sc = list.get(i);
			sc.setSys_LearnPlant(null);
			sc.setClassTime(null);
			sc.setClassTimeShow(null);
			this.updateT(sc);
		} 
		return 1;

	}
	 
	
}
